package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Movies;
import com.itheima.domain.MoviesExample;
import com.itheima.mapper.MoviesMapper;
import com.itheima.service.MoviesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MoviesServiceImpl implements MoviesService {
    @Autowired
    private MoviesMapper moviesMapper;
    @Override
    public PageInfo<Movies> findByPage(Integer pageNum, Integer pageSize, Integer cid, String keyword) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //查询list
        MoviesExample moviesExample = new MoviesExample();
        MoviesExample.Criteria criteria = moviesExample.createCriteria();
        if (cid != null){
            criteria.andCidEqualTo(cid);
        }
        if (keyword != null && keyword.length()>0){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        moviesExample.setOrderByClause("show_time desc");
        List<Movies> list = moviesMapper.selectByExample(moviesExample);
        return new PageInfo<Movies>(list);
    }

    @Override
    public void save(Movies movies) {
        if (movies.getId()!=null){
            //如果有id就是修改
            moviesMapper.updateByPrimaryKeySelective(movies);
        }else{
            //没有id就是新增
            moviesMapper.insert(movies);
        }

    }

    @Override
    public Movies findById(Integer id) {
        return moviesMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Integer id) {
        moviesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteIds(Integer[] ids) {
        moviesMapper.deleteIds(ids);
    }

    @Override
    public List<Movies> findAll() {
        MoviesExample moviesExample = new MoviesExample();
        return moviesMapper.selectByExample(moviesExample);
    }
}
